import { useCallback } from "react";
import { useSelector, useDispatch } from "react-redux"
import Creators from "../store/actions"
// 拿到抢购的api
import seckillApi from "../api/seckill"

export const useResSeckill = () => {
    const seckill = useSelector(state => state.seckill_res)
    const { _id,commodity_id, name, Price, restricted,
        Total, Sold, Start_time, End_time, Sale_type, State
    } = seckill
    const dispatch = useDispatch()
    // 将要新增的数据放入状态机
    const addReSeckills = useCallback((data) => {
        // 将拿到的对象传入状态机
        dispatch(Creators.seckillAddReseckill({ data }))

    }, [commodity_id, name, Price, restricted, Total, Sold, Start_time, End_time, Sale_type, State])
   
    const updataDataToSever =async () => {
        const data = {_id,
            commodity_id, name, Price, restricted,
            Total, Sold, Start_time, End_time, Sale_type, State
        }
        console.log("req",_id,
            commodity_id, name, Price, restricted,
            Total, Sold, Start_time, End_time, Sale_type, State);
      const result= await seckillApi.updataSeckill(data)
      return result
    }

    return [{
        commodity_id, name, Price, restricted,
        Total, Sold, Start_time, End_time, Sale_type, State
    }, { addReSeckills, updataDataToSever }]

}